<template>
  <div class="role-container">
    <div class="left-wrapper">
      <el-button
        class="addBtn"
        size="mini"
        type="primary"
        @click="$router.push('/sys/addRole')"
        style="margin-bottom: 10px"
        >添加角色</el-button
      >

      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="handleRole(index)"
      >
        <div class="role-info">
          <!-- 精灵图高亮 -->
          <svg-icon
            :icon-class="index === activeIndex ? 'user-active' : 'user'"
            class="icon"
          />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="toEdit(item.roleId)"
                >编辑角色</el-dropdown-item
              >
              <el-dropdown-item
                style="color: red"
                @click.native="deliteRole(item.roleId)"
                >删除</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName" style="margin-left: 5px">
        <el-tab-pane label="权限管理" name="first">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <!--绑定活节点 -->
              <!-- 因为保证不了后端的数据和树形结构的字段一致，所以需要使用:props配置 -->
              <!-- default-expand-all 默认展开所有节点 -->
              <!-- show-checkbox 展示复选框 -->
              <!-- 禁用的效果如何做？ -->
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                default-expand-all
                show-checkbox
                node-key="id"
              />
            </div></div
        ></el-tab-pane>
        <el-tab-pane :label="`成员(${total})`" name="user">
          <div class="user-wrapper">
            <el-table :data="userList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  reqGetRoleListAPI,
  reqGetTreeAPI,
  reqGetRolePermisionAPI,
  reqGetRoleUserAPI,
  deleteRoleAPI,
} from "@/api/system";
export default {
  name: "Role",
  data() {
    return {
      total: 0,
      userList: [],
      roleList: [],
      // 存储所有的权限列表
      treeList: [],
      activeIndex: 0,
      activeName: "first",
      // 绑定树形结构的显示，因为显示文本只能识别label
      defaultProps: { label: "title", disabled: () => true },
      params: {
        page: 1,
        pageSize: 10,
      },
    };
  },
  async created() {
    await this.getRoleList(); //等待数据加载完毕再执行下面的操作 async await对内同步，对外异步，因此需要在这添加
    this.getTreeList();
    this.handleRole(0);
  },
  methods: {
    // 角色
    async getRoleList() {
      const { data } = await reqGetRoleListAPI();
      this.roleList = data;
    },
    // 角色改变
    async handleRole(index) {
      console.log(index);
      console.log(index);
      this.activeIndex = index;
      // 根据index获取id进行右侧对应的渲染
      // 获取id
      const roleId = this.roleList[index].roleId;
      // 拿到对应角色的权限
      this.getRoleDetail(roleId);
      this.getRoleUserList(roleId);
    },
    // 获取角色对应的权限列表
    async getRoleDetail(id) {
      const res = await reqGetRolePermisionAPI(id);
      const perms = res.data.perms;
      console.log(perms, "权限列表");
      // 获取数组，其中包括多个tree
      const treeComponentList = this.$refs.tree;
      treeComponentList.forEach((tree, index) => {
        // perm是一个二维数组，选中二维数组的第index个元素的id值
        tree.setCheckedKeys(perms[index]);
      });
    },
    // 树
    async getTreeList() {
      const { data } = await reqGetTreeAPI();
      this.treeList = data;
      // this.addDisabledProp(this.treeList);
    },
    // 运用递归添加disabled
    // forEach 无返回值
    addDisabledProp(treeList) {
      treeList.forEach((item) => {
        // 修改原数组 添加disabled属性
        item.disabled = true;
        if (item.children) {
          this.addDisabledProp(item.children);
        } else {
          return;
        }
      });
    },
    // 获取角色对应的用户信息
    async getRoleUserList(id) {
      const res = await reqGetRoleUserAPI(id, this.params);
      console.log(res);
      this.total = res.data.total;
      this.userList = res.data.rows;
    },
    toEdit(id) {
      this.$router.push({ path: "/sys/addRole", query: { id } });
    },
    // 删除角色
    deliteRole(id) {
      this.$confirm("确认删除吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteRoleAPI(id);
          this.$message.success("删除成功");
          this.getRoleList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }
    ::v-deep(.el-tabs__nav-wrap) {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
